package com.hoas.quickstart.threads.chapter05.chapter05_1_5;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Test9 {

    static class MyTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("begin timer=" + new Date());
            System.out.println("end timer=" + new Date());
        }
    }

    public static void main(String[] args) {
        MyTask task = new MyTask();
        System.out.println("现在执行的时间是: " + new Date());
        Calendar calendarRef = Calendar.getInstance();
        calendarRef.add(Calendar.SECOND, -20);
        Date runDate = calendarRef.getTime();
        System.out.println("计划执行时间: " + runDate);
        Timer timer = new Timer();
//        timer.schedule( task, runDate, 2000);
        timer.scheduleAtFixedRate( task, runDate, 2000);
    }
}
